package com.ogilvy.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ogilvy.cms.bean.CmsLog;
import com.ogilvy.cms.bean.CmsPageVersions;
import com.ogilvy.cms.bean.CmsUser;
import com.ogilvy.cms.util.ServiceDispatcher;
import com.ogilvy.common.JSONBean;
import com.ogilvy.common.MetaData;
import com.ogilvy.common.UtilTools;
import com.ogilvy.frame.util.CmsConstants;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("cms.action.CmsPageVersionsAction")
public class CmsPageVersionsAction extends AdminAction {
	@Autowired
	protected ServiceDispatcher serviceDispatcher;
	private String rows;//前台以JSON的形式传递数据
	private int start;//分页开始
	private int limit;//每页显示量
	private CmsPageVersions cmspageversions = new CmsPageVersions();
	List<CmsPageVersions> datalist;
	//转到页面中
    public String index()
    {
    	return "pageversions";
    }
	
	/** 查询 */
	public void query(){
		//设置页面编码
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		//根据查询条件，实现模糊查询
		if(rows==null){
			datalist = serviceDispatcher.getCmsPageVersionsService().query(cmspageversions);
		}else
		{
			net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
			Map<String,String> screenMap = new HashMap<String, String>();
			screenMap.put("cpveVersionsName",js.getString("cpveVersionsName"));
			datalist = serviceDispatcher.getCmsPageVersionsService().queryByScreen(screenMap);
		}
		
		//datalist = serviceDispatcher.getCmsDictionaryService().query(cmsdictionary);
		//采用集合分页
		limit = datalist.size() > (start+limit)?(start+limit):(datalist.size()-start);
		List pageList = UtilTools.queryForPage(start, limit, datalist);
		JSONBean jbean = new JSONBean();
		MetaData md = new MetaData();

		md.setIdProperty("cpveId");//关联将要显示列表对应的数据字典bean实体的主键ID
		md.reflectFields("com.ogilvy.cms.bean.CmsPageVersions");//关联将要显示列表对应的bean实体
		jbean.setMetaData(md);
		jbean.setRows(pageList);
		jbean.setSuccess(true);
		jbean.setTotal(datalist.size());	//此处是datalist

		//String jsonString = UtilTools.encodeObject2Json(jbean);
		String jsonString = UtilTools.encodeObject2Json(jbean,"MM/dd/yyyy HH:mm:ss");
		
		
		
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//return "query";
	}
	
	/** 添加 */
	public void add(){
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		Integer cpveCmpId = js.getInt("cpveCmpId");							//网页ID
		String cpveVersionsName = js.getString("cpveVersionsName");			//版本名称
		String cpveCmpContent = js.getString("cpveCmpContent");				//页面数据
		String cpveComments = js.getString("cpveComments");					//说明
		
		cmspageversions.setCpveCmpId(cpveCmpId);
		cmspageversions.setCpveVersionsName(cpveVersionsName);
		cmspageversions.setCpveCmpContent(cpveCmpContent);
		cmspageversions.setCpveComments(cpveComments);
		//添加人
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String	cmdAdduser = cmsuser.getCmuName();
		cmspageversions.setCpveAdduser(cmdAdduser);			//将当前用户添加到cmspageversions对象中
		//添加时间
		Date cmdAddtime = UtilTools.getCurrrentDate1();	
		cmspageversions.setCpveAddtime(cmdAddtime);
		
		CmsPageVersions cmsPageVersions = serviceDispatcher.getCmsPageVersionsService().add(cmspageversions);
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		// 需要新建一个具有id的vo返回给前台以便通知前台保存成功，不然前台一直处于编辑状态
		List rows = new ArrayList();
		rows.add(cmsPageVersions);
		bean.setRows(rows);
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		/**
		 * 系统日志
		 */
		CmsLog  log = new CmsLog();
		log.setCmlTime(UtilTools.getCurrrentDate1());
		log.setCmlFunction("页面版本");
		log.setCmlType(CmsLog.Add);
		log.setCmlDesc("添加");
		log.setCmlUser(cmdAdduser);
		serviceDispatcher.getCmsLogService().add(log);
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//return "detail";
	}
	
	/** 修改 */
	public void modify(){
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		//需要修改的项
		Integer cpveId = js.getInt("cpveId");
		Integer cpveCmpId = js.getInt("cpveCmpId");
		String cpveVersionsName = js.getString("cpveVersionsName");
		String cpveCmpContent = js.getString("cpveCmpContent");
		String cpveComments = js.getString("cpveComments");
//		Integer cpveAddtime = js.getInt("cpveAddtime");
		String cpveAdduser = js.getString("cpveAdduser");
		
		//根据用户选择的数据字典ID，将查询出来.ID为空。先查询出已经存在的属性，然后对需要修改的属性进行修改
		cmspageversions = serviceDispatcher.getCmsPageVersionsService().get(cpveId);
		
		cmspageversions.setCpveId(cpveId);
		cmspageversions.setCpveCmpId(cpveCmpId);
		cmspageversions.setCpveVersionsName(cpveVersionsName);
		cmspageversions.setCpveCmpContent(cpveCmpContent);
		cmspageversions.setCpveComments(cpveComments);
		cmspageversions.setCpveAdduser(cpveAdduser);
		//修改人，从session中获取
//		CmsUser  cmsuser = (CmsUser) ServletActionContext.getRequest().getSession().getAttribute("cmsuser");
//		String	cmdLastuser = cmsuser.getCmuName();
//		cmspageversions.setCmdLastuser(cmdLastuser);
		
		//修改时间，重新获取当前系统时间
//		Date getDate = new Date();
//		Date cmdLasttime = getDate;
//		cmspageversions.setCmdLasttime(cmdLasttime);
		
		
		//执行修改
		serviceDispatcher.getCmsPageVersionsService().modify(cmspageversions);
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		/**
		 * 系统日志
		 */
		CmsLog  log = new CmsLog();
		log.setCmlTime(UtilTools.getCurrrentDate1());
		log.setCmlFunction("页面版本");
		log.setCmlType(CmsLog.MODIFY);
		log.setCmlDesc("修改");
		log.setCmlUser(cpveAdduser);
		serviceDispatcher.getCmsLogService().add(log);
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return "detail";
	}
	
	private Integer cpveId;
	public Integer getCpveId() {
		return cpveId;
	}

	public void setCpveId(Integer cpveId) {
		this.cpveId = cpveId;
	}

	public CmsPageVersions getCmspageversions() {
		return cmspageversions;
	}

	public void setCmspageversions(CmsPageVersions cmspageversions) {
		this.cmspageversions = cmspageversions;
	}


	public List<CmsPageVersions> getDatalist() {
		return datalist;
	}

	public void setDatalist(List<CmsPageVersions> datalist) {
		this.datalist = datalist;
	}

	/** 删除 */
    public void del(){
    	net.sf.json.JSONArray objArr = net.sf.json.JSONArray.fromObject(rows);
        for(int i=0;i<objArr.size();i++)
        {
        	net.sf.json.JSONObject js = objArr.getJSONObject(i);
        	//将 String 转化为 Integer 删除
        	String cpveid = js.getString("cpveId");
        	Integer cpveId = Integer.valueOf(cpveid);
            serviceDispatcher.getCmsPageVersionsService().del(cpveId);
        }
        //获取数据
        StringBuffer jsonString =new StringBuffer();
        jsonString.append("{");
        jsonString.append("success:true,");
        jsonString.append("msg:'删除成功'");
        jsonString.append("}");
        
		/**
		 * 系统日志
		 */
		CmsLog  log = new CmsLog();
		log.setCmlTime(UtilTools.getCurrrentDate1());
		log.setCmlFunction("页面版本");
		log.setCmlType(CmsLog.DELETE);
		log.setCmlDesc("删除");
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String	cmdAdduser = cmsuser.getCmuName();
		log.setCmlUser(cmdAdduser);
		serviceDispatcher.getCmsLogService().add(log);
		
        PrintWriter out;
        try {
        	ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
            out = ServletActionContext.getResponse().getWriter();
            out.print(jsonString.toString());
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	
	
	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
}
